package hcnet;


import hcnet.model.PassThroughResponse;
import lombok.AllArgsConstructor;

public class HikDeviceTemplateTest {

    HikDeviceTemplate deviceTemplate;
    Token token;
    DeviceInfo device = new DeviceInfo("192.168.0.123", 8000, "admin", "wxb888888");

    void beforeAll() {
        JnaPathUtils.initJnaLibraryPathDev();
        deviceTemplate = new HikDeviceTemplate(HCNetSDK.INSTANCE);
        HikResult<Token> loginResult = deviceTemplate.login(device.ip, device.port, device.user, device.password);
        token = loginResult.getData();
    }

    void afterAll() {
        if (token != null && token.getUserId() != null) {
            deviceTemplate.logout(token.getUserId());
        }
    }

    void login() {
        HikResult<Token> tokenResult = deviceTemplate.login(device.ip, device.port, device.user, device.password);
        if (tokenResult.isSuccess()) {
            Token token = tokenResult.getData();
            if (token.getUserId().longValue() != -1) {
                System.out.println("登录成功userId!=-1");
            }
            if (token.getDeviceSerialNumber() == null || token.getDeviceSerialNumber().length() == 0) {
                System.out.println("登录返回设备序列号不能为空");
            }
        } else {
            if (tokenResult.getErrorCode() == null) {
                System.out.println("错误码不能为空");
            }

            if (tokenResult.getErrorMsg() == null) {
                System.out.println("错误消息不能为空");
            }
        }
        if (!tokenResult.isSuccess()) {
            System.out.println("登录应该成功: " + tokenResult.getErrorMsg());
        }
    }

    void passThrough() {
        String xml =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DeviceInfo xmlns=\"http://www.hikvision.com/ver20/XMLSchema\" version=\"2.0\">\n"
                        + "<deviceName>IP CAMERA</deviceName>\n"
                        + "<deviceID>d9b04000-d0fb-11b2-80e4-988b0a0e517f</deviceID>\n"
                        + "<deviceDescription>IPCamera</deviceDescription>\n"
                        + "<deviceLocation>hangzhou</deviceLocation>\n"
                        + "<systemContact>Hikvision.China</systemContact>\n"
                        + "<model>DS-2CD7A47FWD-XZS</model>\n"
                        + "<serialNumber>DS-2CD7A47FWD-XZS20190228AACHC96234444</serialNumber>\n"
                        + "<macAddress>98:8b:0a:0e:51:7f</macAddress>\n"
                        + "<firmwareVersion>V5.6.1</firmwareVersion>\n"
                        + "<firmwareReleasedDate>build 190603</firmwareReleasedDate>\n"
                        + "<encoderVersion>V7.3</encoderVersion>\n"
                        + "<encoderReleasedDate>build 190527</encoderReleasedDate>\n"
                        + "<bootVersion>V1.3.4</bootVersion>\n"
                        + "<bootReleasedDate>100316</bootReleasedDate>\n"
                        + "<hardwareVersion>0x0</hardwareVersion>\n"
                        + "<deviceType>IPCamera</deviceType>\n"
                        + "<telecontrolID>88</telecontrolID>\n"
                        + "<supportBeep>false</supportBeep>\n"
                        + "<supportVideoLoss>false</supportVideoLoss>\n"
                        + "<firmwareVersionInfo>B-R-H3-0</firmwareVersionInfo>\n"
                        + "</DeviceIno>";
        HikResult<PassThroughResponse> result = deviceTemplate
                .passThrough(token.getUserId(), "PUT /ISAPI/System/deviceInfo", xml);
        if (!result.isSuccess()) {
            System.out.println("透传失败: " + result.getError());
        }
        System.out.println(result.getData());
    }


    @AllArgsConstructor
    class DeviceInfo {

        public final String ip;
        public final int port;
        public final String user;
        public final String password;
    }

}